package com.java2104.controller;

import com.java2104.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/getInfo")
    //如果返回对象的JSON格式，需要在注解方法前添加@RsponseBody
    @ResponseBody
    public User getInfo(){
        User user=new User();
        user.setUsername("liuhua");
        user.setPassword("5467890");
        user.setNickName("刘德华");
        user.setState(1);
        user.setLastLogin(new Timestamp(System.currentTimeMillis()));
        return user;
    }
    @RequestMapping("/updatePassword")
    //localhost:8080/user/updatePassword?user_id=1002&old_pass=123456&new_pass=567890
    public String updatePassword(Model model, @RequestParam("user_id") String userId,
                                 @RequestParam("old_pass") String oldPass,
                                 @RequestParam("new_pass") String newPass,
                                 @RequestAttribute("token")String token){
        model.addAttribute("userid",userId);
       // model.addAttribute("old_pass",oldPass);
        model.addAttribute("newpass",newPass);
        model.addAttribute("token",token);
        return "updatePassword";

    }

    @RequestMapping("/getInfo2")
    //如果返回对象的JSON格式，需要在注解方法前添加@RsponseBody
    @ResponseBody
    public User getInfo2(){
        return getInfo();
    }
}
